package com.neuedu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Dept;
import com.neuedu.mapper.DeptMapper;
import com.neuedu.service.DeptService;
import com.neuedu.vo.DeptCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Override
    public Page<Dept> getDeptList(DeptCondition deptCondition) {

        Page page = new Page(deptCondition.getCurrentPage(), deptCondition.getPageSize());

        if(deptCondition.getDeptType()==null){  //全部科室类型
            return this.query().like("dept_name", deptCondition.getDeptName()).page(page);
        }else{
            return this.query()
                    .like("dept_name", deptCondition.getDeptName())
                    .eq("dept_type", deptCondition.getDeptType())
                    .page(page);
        }

    }

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public boolean removeEnable(int deptId) {
        return deptMapper.findDoctorCount(deptId)==0;
    }

    @Override
    public boolean deleteBatch(String deptIds) {

        //将字符串拆分成数组  "1,2,3" --> [1,2,3]
        String[] arr = deptIds.split(",");

        //字符串数组转换集合  [1,2,3] --> {1,2,3}
        List<Integer> list = new ArrayList<>();

        for (String s : arr) {
            list.add(Integer.parseInt(s));
        }

        return this.removeByIds(list);
    }


}
